package com.feihong.yw189.paidan.qiangdanservice;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.paidan.bean.PaiDanStateInfo;
import com.google.gson.Gson;

import org.xutils.common.Callback;

/**
 * Created by admin on 2018/2/3.
 */

public class JieDanTools {

    private static final String TAG = "派单";
    private Context mContext;
    private JieDanInterface mJieDanInterface;
    private LocationToolsOnces locationTools;

    public void stopLocation() {
        if (locationTools != null) {
            locationTools.stopLocation();
        }
    }

    public JieDanTools(Context context) {
        this.mContext = context;
    }


    public synchronized void stopJieDan() {

    }


    /***
     * 修改状态
     * @param jieDanInterface
     */
    public synchronized void setCurrentState(JieDanInterface jieDanInterface) {
        if (jieDanInterface == null) {
            return;
        }
        if (locationTools != null) {
            locationTools.stopLocation();
        }
        mJieDanInterface = jieDanInterface;
        locationTools = new LocationToolsOnces(mContext, new LocationCallBack() {
            @Override
            public void successGetLocation(LocationInfo info) {
                locationTools.stopLocation();
                Log.e("项目异常", "------successGetLocation------" + "setState");
                if (info == null) {
                    mJieDanInterface.result(-5, "获取位置失败1!");
                    return;
                }
                String Latitude = info.getLatitude() + "";
                String Longitude = info.getLongitude() + "";
                if (TextUtils.isEmpty(Latitude) || TextUtils.isEmpty(Longitude)) {
                    mJieDanInterface.result(-5, "获取位置失败2");
                    return;
                }
                setState(Latitude, Longitude);
            }

            @Override
            public void error(String error) {
                Log.e("项目异常", "------1------" + error);
                locationTools.stopLocation();
                mJieDanInterface.result(-5, "获取位置失败,请检查APP定位权限!");
                return;
            }
        });
        locationTools.startLocation(0);

    }

    private void setState(String Latitude, String Longitude) {

        App.getApp().getHttpTools().setState(Latitude, Longitude, new RequestCallBack<String>() {
            @Override
            public void onSuccess(String s) {

                Log.e(TAG, "JieDanTools=setState==onSuccess: S=" + s);
                try {
                    PaiDanStateInfo paiDanStateInfo = new Gson().fromJson(s, PaiDanStateInfo.class);
                    if (paiDanStateInfo == null) {
                        mJieDanInterface.result(-1, "无法更改状态");
                        return;
                    }
                    if (paiDanStateInfo.getStatus() == 0) {
                        mJieDanInterface.result(-1, "无法更改状态");
                    } else {
                        String ltsc_is_open = paiDanStateInfo.getData().getLtsc_is_open();
                        int result = Integer.parseInt(ltsc_is_open);
                        mJieDanInterface.result(result, "状态获取成功");
                    }
                } catch (Exception e) {
                    mJieDanInterface.result(-1, "无法更改状态");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "------err------" + "网络错误:" + ex);
                mJieDanInterface.result(-1, "无法更改状态");
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    /***
     * 获取用户当前的状态 1 是开启状态  2 表示关闭状态  其他表示 获取失败
     * @param jieDanInterface 回调
     */
    public synchronized void getCurrentStatus(JieDanInterface jieDanInterface) {
        if (jieDanInterface == null) {
            return;
        }
        mJieDanInterface = jieDanInterface;

        App.getApp().getHttpTools().getState(new RequestCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "JieDanTools==getState=onSuccess: S=" + s);
                try {
                    PaiDanStateInfo paiDanStateInfo = new Gson().fromJson(s, PaiDanStateInfo.class);
                    if (paiDanStateInfo == null) {
                        mJieDanInterface.result(-1, "无法更改状态");
                        return;
                    }
                    if (paiDanStateInfo.getStatus() == 0) {
                        mJieDanInterface.result(-1, "无法更改状态");
                    } else {
                        String ltsc_is_open = paiDanStateInfo.getData().getLtsc_is_open();
                        int result = Integer.parseInt(ltsc_is_open);
                        mJieDanInterface.result(result, "状态获取成功");
                    }
                } catch (Exception e) {
                    mJieDanInterface.result(-1, "无法获取状态");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "------err------" + "网络错误:" + ex);
                mJieDanInterface.result(-1, "无法获取状态");
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }


}
